package cn.lili.task;

import cn.lili.modules.payment.kit.plugin.alipay.AliPayApi;
import cn.lili.modules.store.entity.dos.StoreZft;
import cn.lili.modules.store.service.StoreZftService;
import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.ZftSubMerchantOrder;
import com.alipay.api.response.AntMerchantExpandIndirectZftorderQueryResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

@Configuration
@EnableScheduling
@Slf4j
public class StoreZftTask {
    @Autowired
    private StoreZftService storeZftService;

    @Scheduled(cron = "0 0/2 * * * ?")
    public void a() {
        List<StoreZft> list = storeZftService.findAudit();
        log.info("....直付通任务调度");
        if (CollectionUtils.isNotEmpty(list)) {

            for (StoreZft storeZft : list) {
                try {
                    AntMerchantExpandIndirectZftorderQueryResponse response = AliPayApi.zftOrderQuery(storeZft.getExternalId(), storeZft.getOrderId());
                    if (response.isSuccess()) {
                        List<ZftSubMerchantOrder> orders = response.getOrders();
                        ZftSubMerchantOrder order = orders.get(0);

                        storeZft.setStatus(order.getStatus());
                        storeZft.setApplyTime(order.getApplyTime());
                        storeZft.setApplyType(order.getApplyType());
                        storeZft.setFkAudit(order.getFkAudit());
                        storeZft.setFkAuditMemo(order.getFkAuditMemo());
                        storeZft.setKzAudit(order.getKzAudit());
                        storeZft.setKzAuditMemo(order.getKzAuditMemo());
                        storeZft.setSubConfirm(order.getSubConfirm());
                        storeZft.setCardAliasNo(order.getCardAliasNo());
                        storeZft.setSmid(order.getSmid());
                        storeZft.setAppPreAuth("TRUE".equals(order.getAppPreAuth()));
                        storeZft.setFacePreAuth("TRUE".equals(order.getFacePreAuth()));
                        storeZft.setIsFaceLimit("TRUE".equals(order.getIsFaceLimit()));
                        storeZft.setReason(storeZft.getReason());

                        storeZftService.updateById(storeZft);
                    }
                } catch (AlipayApiException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
